package org.example.controller;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.service.FamilyService;
import org.example.service.UserService;
import org.example.utils.ElderlyHealthResponseData;
import org.example.utils.FamilyUpdateRequest;
import org.example.utils.R;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: FamilyController
 * Description:
 *
 * @Author Kotaro
 * @Create 2024/12/1 15:02
 * @Version 1.0
 */
@CrossOrigin //允许其他源访问我们的controller
@RestController
@RequestMapping("family")
@Slf4j
public class FamilyController {
    @Resource
    private FamilyService familyService;
    @Resource
    private UserService userService;

    @GetMapping("health")//根据家属绑定的老人ID查询该老人的健康状态
    public R selectElderlyHealthByFamilyId(@RequestParam String familyId,
                                           @RequestParam String elderlyId){

        boolean hasElderlyIdByFamilyId = familyService.isElderlyBoundToFamily(familyId , elderlyId);
        if(hasElderlyIdByFamilyId){
            ElderlyHealthResponseData elderlyHealthResponseData = userService.selectElderlyHealthByElderlyId(elderlyId);
            return R.successful("查询成功！" , elderlyHealthResponseData);
        }else {
            return R.error("查询失败！未绑定该老人ID" , null);
        }
    }

    @PutMapping("update")
    public R updateFamilyByFamilyId(@RequestBody FamilyUpdateRequest familyUpdateRequest){
        R r = familyService.updateFamilyByFamilyId(familyUpdateRequest);
        return r;
    }

}
